package cn.wolfcode.car.business.service.impl;


import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.query.AppointmentQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AppointmentServiceImpl implements IAppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;

    @Override
    public TablePageInfo<Appointment> query(AppointmentQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Appointment>(appointmentMapper.selectForList(qo));
    }


    @Override
    public void save(Appointment appointment) {
        if (appointmentMapper.queryByPhoneAndCar(appointment.getCustomerPhone(),appointment.getLicensePlate())!=null) {
            throw new BusinessException("该用户已经存在");
        }

        appointment.setCreateTime(new Date());
        appointment.setIsdelete(Appointment.ISDELETE_FALSE);
        appointment.setStatus(Appointment.STATUS_APPOINTMENTS);
        if(appointment.getSource()==null){
            appointment.setSource(Appointment.SOURCE_NET);
        }
        appointmentMapper.insert(appointment);
    }

    @Override
    public Appointment get(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Appointment appointment) {

        Appointment appointment1 = this.get(appointment.getId());

        if(!Appointment.STATUS_APPOINTMENTS.equals(appointment1.getStatus())){
            throw new BusinessException("预约订单非预约状态无法修改");
        }
        appointmentMapper.updateByPrimaryKey(appointment);
    }

    @Override
    public void deleteBatch(String ids) {
        if(ids==null) {
            throw new BusinessException("参数不为空");
        }
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            Appointment appointment = this.get(dictId);
            if(!Appointment.STATUS_APPOINTMENTS.equals(appointment.getStatus())){
                throw new BusinessException("预约订单非预约状态无法删除");
            }
            appointmentMapper.deleteUpdateByPrimaryKey(dictId);
        }
    }

    @Override
    public List<Appointment> list() {
        return appointmentMapper.selectAll();
    }

    @Override
    public void arrive(Long id) {
        Appointment appointment = this.get(id);
        if(!Appointment.STATUS_APPOINTMENTS.equals(appointment.getStatus())){
            throw new BusinessException("预约订单非预约状态无法修改状态");
        }
        appointmentMapper.changStatus(id,Appointment.STATUS_ARRIVE,new Date());
    }

    @Override
    public void cancel(Long id) {
        Appointment appointment = this.get(id);
        if(!Appointment.STATUS_APPOINTMENTS.equals(appointment.getStatus())){
            throw new BusinessException("预约订单非预约状态无法取消");
        }
        appointmentMapper.changStatus(id,Appointment.STATUS_USERCANCELLED,null);
    }

    @Override
    public void payStatement(Long appointmentId) {
        appointmentMapper.changStatus(appointmentId,Appointment.STATUS_PAID,null);
    }


}
